package com.bj.disc.util

import android.animation.*
import android.view.View
import com.blankj.utilcode.util.ConvertUtils

object AnimationUtils {
    private var animationList: ArrayList<AnimatorSet> = ArrayList<AnimatorSet>()

    fun startAnimation(view: View, targetView: View) {

        // 计算目标位置的坐标
        val targetX = targetView.right.toFloat()
        val targetY = targetView.top.toFloat() - ConvertUtils.dp2px(50f)

        // 创建动画对象，将 view 移动到目标位置
        val translationX = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, targetX)
        val translationY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, targetY)

        // 设置初始状态为隐藏
        view.visibility = View.INVISIBLE

        // 设置动画监听器，在动画结束时显示 view
        val animatorListener = object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.VISIBLE
            }
        }

        var animatorSet = AnimatorSet()
        animatorSet?.playTogether(translationX, translationY)
        animatorSet?.duration = 500 // 动画持续时间，单位为毫秒
        animatorSet?.addListener(animatorListener)
        animatorSet?.start()
        animationList.add(animatorSet)
    }

    fun animateAndDisappear(view: View) {
        // 获取View的中心点坐标
        stopAnimation()
        val centerX = (view.left + view.right) / 2
        val centerY = (view.top + view.bottom) / 2

        // 创建横向缩放动画
        val scaleXAnimator = ObjectAnimator.ofFloat(view, View.SCALE_X, 1f, 0f)
        scaleXAnimator.duration = 500

        // 创建纵向缩放动画
        val scaleYAnimator = ObjectAnimator.ofFloat(view, View.SCALE_Y, 1f, 0f)
        scaleYAnimator.duration = 500

        // 创建透明度动画
        val alphaAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f)
        alphaAnimator.duration = 500

        // 创建动画集合
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(scaleXAnimator, scaleYAnimator, alphaAnimator)

        // 设置动画执行的中心点
        view.pivotX = centerX.toFloat()
        view.pivotY = centerY.toFloat()

        // 监听动画结束事件
        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                // 动画结束后，隐藏View
//                view.alpha = 1f
//                view.scaleX = 1f
//                view.scaleY = 1f
                view.visibility = View.GONE


            }
        })

        // 开始执行动画
        animatorSet.start()
        animationList.add(animatorSet)
    }

    fun startAnimation(view: View) {
        stopAnimation() // 停止之前的动画

        val scaleX = ObjectAnimator.ofFloat(view, View.SCALE_X, 1f, 0f)
        val scaleY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 1f, 0f)
        val translationX =
            ObjectAnimator.ofFloat(view, View.TRANSLATION_X, 0f, -view.width.toFloat())
        val translationY =
            ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, 0f, -view.height.toFloat())

        var animatorSet = AnimatorSet()
        animatorSet?.playTogether(scaleX, scaleY, translationX, translationY)
        animatorSet?.duration = 1500 // 动画持续时间，单位为毫秒
        animatorSet?.start()
        animationList.add(animatorSet)
    }

    fun fadeAnimation(view: View) {
        stopAnimation() // 停止之前的动画

        // 创建透明度动画对象，将 view 渐变为透明
        val alphaAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f)

        // 设置动画监听器，在动画结束时隐藏 view
        val animatorListener = object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.INVISIBLE
            }
        }

        var animatorSet = AnimatorSet()
        animatorSet?.play(alphaAnimator)
        animatorSet?.duration = 1500 // 动画持续时间，单位为毫秒
        animatorSet?.addListener(animatorListener)
        animatorSet?.start()
        animationList.add(animatorSet)
    }
    fun fadeWithNoStopAnimation(view: View) {

        // 创建透明度动画对象，将 view 渐变为透明
        val alphaAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f)

        // 设置动画监听器，在动画结束时隐藏 view
        val animatorListener = object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.INVISIBLE
            }
        }

        var animatorSet = AnimatorSet()
        animatorSet?.play(alphaAnimator)
        animatorSet?.duration = 1500 // 动画持续时间，单位为毫秒
        animatorSet?.addListener(animatorListener)
        animatorSet?.start()
        animationList.add(animatorSet)
    }
    fun fadeAnimation(view: View,duration:Long) {
        stopAnimation() // 停止之前的动画

        // 创建透明度动画对象，将 view 渐变为透明
        val alphaAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f)

        // 设置动画监听器，在动画结束时隐藏 view
        val animatorListener = object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.INVISIBLE
            }
        }

        var animatorSet = AnimatorSet()
        animatorSet?.play(alphaAnimator)
        animatorSet?.duration = duration // 动画持续时间，单位为毫秒
        animatorSet?.addListener(animatorListener)
        animatorSet?.start()
        animationList.add(animatorSet)
    }
    fun fadeAnimation(views: List<View>) {
        stopAnimation() // 停止之前的动画

        val alphaHolder = PropertyValuesHolder.ofFloat(View.ALPHA, 1f, 0f)

        // 设置动画监听器，在动画结束时隐藏 view
        val animatorListener = object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                for (view in views) {
                    view.visibility = View.INVISIBLE
                }
            }
        }

        var animatorSet = AnimatorSet()
        val animatorList = ArrayList<Animator>()
        for (view in views) {
            // 设置初始状态为可见
            view.visibility = View.VISIBLE

            animatorList.add(ObjectAnimator.ofPropertyValuesHolder(view, alphaHolder))
        }
        animatorSet?.playTogether(animatorList)
        animatorSet?.duration = 500 // 动画持续时间，单位为毫秒
        animatorSet?.addListener(animatorListener)
        animatorSet?.start()
        animationList.add(animatorSet)
    }

    //在页面销毁的时候一定要调用这个方法
    fun stopAnimation() {
        animationList.forEach {
            it.cancel()
        }
        animationList.clear()
    }
}
